package com.renshine.swing.listener;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.renshine.base.bean.Assemble;
import com.renshine.base.bean.User;
import com.renshine.base.service.QueryService;
import com.renshine.base.util.Configuration;
import com.renshine.base.util.DataUtil;
import com.renshine.base.util.UserUtil;
import com.renshine.swing.component.dialog.CreateJDialog;
import com.renshine.swing.component.dialog.LoginJDialog;

@Component
public class AttachListenerFactory implements InitializingBean{

	@Autowired
	private LoginJDialog loginJDialog;
	@Autowired
	private CreateJDialog createJDialog;
	@Autowired
	private QueryService service;
	
	@Override
	public void afterPropertiesSet() throws Exception {
		
		// 添加登录拦截器
		loginJDialog.getSubmitPane().getLoginButton().addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// 登录
				User user = loginJDialog.getLoginPanel().getUser();
				if (!user.isWhole()) {
					JOptionPane.showMessageDialog(null, "登录失败, 输入信息不完整~", "错误信息",JOptionPane.ERROR_MESSAGE);
					return;
				}
				boolean bool = UserUtil.login(user);
				if (!bool) {
					JOptionPane.showMessageDialog(null, "登录失败, 请检查您的输入信息~", "错误信息",JOptionPane.ERROR_MESSAGE);
					return;
				}
				// 登录成功后隐藏登录窗口，显示代码创建窗口
				loginJDialog.setVisible(false);
				createJDialog.getInitPanel().loadAndAddListener(null);
				createJDialog.getButtonPanel().init(Configuration.getHasAssemble());
				createJDialog.setVisible(true);
				// 保存登录信息
				Configuration.saveLoginUser(user);
			}
		});
		
		// 选项卡监听器
		createJDialog.getTabbedPanel().addChangeListener(new ChangeListener() {
			@Override
			public void stateChanged(ChangeEvent e) {
				int seleced = createJDialog.getTabbedPanel().getSelectedIndex();
				if (seleced == 0) return;
				// 取得选中的表的值
				List<String> tbNames = DataUtil.getSelectedTbNames(createJDialog.getInitPanel().getModel());
				switch (seleced) {
				case 1:
					createJDialog.getEditPanel().loadAndAddListener(tbNames);
					break;
				case 2:
					createJDialog.getSearchPanel().loadAndAddListener(tbNames);
					break;
				case 3:
					createJDialog.getListPanel().loadAndAddListener(tbNames);
					break;
				default:
					break;
				}
			}
		});
		
		// 选择路径监听器
		createJDialog.getButtonPanel().getSltButton().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFileChooser chooser = new JFileChooser();
				chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				if(chooser.showOpenDialog(createJDialog) == JFileChooser.APPROVE_OPTION){
					createJDialog.getButtonPanel().getPathText().setText(chooser.getSelectedFile().getPath());
				}
			}
		});
		
		// 生成代码监听器
		createJDialog.getButtonPanel().getCreateButton().addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// 数据验证
				String author = createJDialog.getButtonPanel().getAuthorField().getText();
				if (author == null || author.equals("")) {
					JOptionPane.showMessageDialog(createJDialog, "作者不能为空~", "错误提示",JOptionPane.ERROR_MESSAGE);
					return;
				}
				String module = createJDialog.getButtonPanel().getModuleText().getText();
				if (module == null || module.equals("")) {
					JOptionPane.showMessageDialog(createJDialog, "所属模块不能为空~", "错误提示",JOptionPane.ERROR_MESSAGE);
					return;
				}
				String path = createJDialog.getButtonPanel().getPathText().getText();
				if (path == null || path.equals("")) {
					JOptionPane.showMessageDialog(createJDialog, "请选择生成路径~", "错误提示",JOptionPane.ERROR_MESSAGE);
					return;
				}
				
				String template = createJDialog.getButtonPanel().getTemplateBox().getSelectedItem().toString();
				// 数据获取
				Assemble assemble = DataUtil.assemble(author, module, path,template, createJDialog);
				if (assemble.getItems().isEmpty()) {
					JOptionPane.showMessageDialog(createJDialog, "未配置任何信息~", "错误提示",JOptionPane.ERROR_MESSAGE);
					return;
				}
				
				// 保存常用的配置
				Configuration.saveAssemble(assemble);
				
				// 代码生成
				try {
					service.createCode(assemble);
					// 成功提示
					JOptionPane.showMessageDialog(createJDialog, "代码已生成完毕！", "成功信息",JOptionPane.INFORMATION_MESSAGE);
				} catch (Exception e2) {
					// 成功提示
					JOptionPane.showMessageDialog(createJDialog, "代码生成失败，请查看错误日志！", "错误信息",JOptionPane.ERROR_MESSAGE);
				}
				
			}
		});
	}
	
}
